package com.caolei1992.impl.business.system.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caolei1992.api.business.system.entity.User;
import com.caolei1992.api.business.system.service.IUserService;
import com.caolei1992.impl.business.system.dao.UserDao;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 系統用戶表 服务实现类
 * </p>
 *
 * @author caolei
 * @since 2019-12-04
 * <p>
 * <p>
 * Service添加group是设置隔离的，假如一个接口有多个实现方式，对应需要指定调用，可以设置group,对应，
 * 引入也需要设置 eg:@Reference(group = "user"),
 * version是设置版本的，控制版本级别，用法与group一致
 * 如果没有多个实现方式 ，默认可以不做设置
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {
    private final String USER_CACHE_NAME = "users_test";

    @Override
    @Cacheable(value = USER_CACHE_NAME, key = "'user_' + #id")
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public User findById(String id) {
        return getById(id);
    }

    @Override
    @CacheEvict(value = USER_CACHE_NAME, key = "'user_' + #id")
    public boolean deleteById(String id) {
        return removeById(id);
    }

    @Override
    @CacheEvict(value = USER_CACHE_NAME, key = "'user_' + #user.id")
    public boolean update(User user) {
        return updateById(user);
    }
}
